package cn.ococci.dm.Main;

import com.alibaba.fastjson.JsonObject;
import com.loopj.android.http.RequestParams;

import org.litepal.crud.DataSupport;


import java.util.List;

import cn.ococci.dm.Base.ActivityStack;
import cn.ococci.dm.Base.BaseActivity;
import cn.ococci.dm.Main.Activity.MainActivity;
import cn.ococci.dm.Main.Model.CrashData;
import cn.ococci.dm.Main.Model.CurUser;
import cn.ococci.dm.Main.Model.User;
import cn.ococci.dm.Main.Tools.DbTool;
import cn.ococci.dm.Main.Tools.LogTool;
import cn.ococci.dm.Main.Tools.SpTool;
import cn.ococci.dm.Main.Tools.ToastTool;
import cn.ococci.dm.Network.Http;
import cn.ococci.dm.Network.HttpCallback;
import cn.ococci.dm.Network.Urls;
import cn.ococci_dm.R;

/**
 * Created by cyan on 15/12/29.
 */
public class UserBiz {

    /**
     * 登录
     * @param ba
     * @param account
     * @param pwd
     */
    public static void signIn(final BaseActivity ba, final String account, String pwd) {
        RequestParams p = new RequestParams();
        p.put(Const.BuilderParams.user, account);
        p.put(Const.BuilderParams.pass, pwd);
        p.put(Const.BuilderParams.device_type,"0");
        Http.post(Urls.login, p, new Http(ba) {
            @Override
            public void onSuccess(JsonObject json) {
                User user = Convert.jsonToUser(json, account);

                if(user.getCompanyId() == -1){
                    ToastTool.show(R.string.login_manager);
                    return;
                }


                if (DataSupport.findFirst(User.class) == null) {
                    user.save();
                } else {
                    user.update(1);
                }

                SpTool.putBoolean(Const.LoginParams.ON_LINE, true);
                SpTool.putString(Const.LoginParams.USER_ACCOUNT, account); //保存账号
                SpTool.setLoginFlag(true);  //登录的标志标志位

                ba.turnTo(MainActivity.class);
            }

            @Override
            public void onFailure(int code, String msg) {
                if (code == 0) {
                    ToastTool.show(R.string.net_from_keep_unimpeded);
                } else if (code == -1 || code == -2) {
                    ToastTool.show(R.string.login_errors);
                } else {
                    LogTool.log(LogTool.CYAN, "服务器出现问题....");
                }

            }
        });
    }


    public static void uploadErrorLog(boolean isBack, List<CrashData> crashDataList, final HttpCallback callback){

        RequestParams requestParams = Convert.crashDataListToParams(crashDataList);
        requestParams.put(Const.BuilderParams.id, CurUser.getPhone());

        Http http = new Http(ActivityStack.currentActivity()){

            @Override
            public void onSuccess(JsonObject json) {
                callback.onSuccess(json);
            }
        };

        if (isBack){
            http.turnToBack();
        }

        Http.post(Urls.requestErrorLog,requestParams,http);

    }





    /**
     * 注销方法
     */
    public static void logout() {

        SpTool.clearAllData();
        DbTool.clearDb();
        ActivityStack.doLogout();
        CurUser.clear();

    }

}
